package cn.kshost.zqyg.manager.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.kshost.zqyg.manager.properties.MinioProperties;
import cn.kshost.zqyg.manager.service.FileUploaderService;
import io.minio.*;
import io.minio.errors.*;
import okhttp3.MultipartBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;

@Service
public class FileUploaderServiceImpl  implements FileUploaderService {
    @Autowired
    MinioProperties minioProperties;
    public String  upLoadAvatar(MultipartFile file)  {
        String url = null;
        try {
            //获取上传文件流
            InputStream inputStream = file.getInputStream();
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint(minioProperties.getEndpoint())
                            .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                            .build();
            // 检测桶是否存在
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucket()).build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucket()).build());
            } else {
                System.out.println("Bucket "+minioProperties.getBucket()+" already exists.");
            }

            String dirPath = DateUtil.format(new Date(),"yyyyMMdd");
            String fileName  =  dirPath+"/"+ UUID.randomUUID().toString().replace("-","")+file.getOriginalFilename();
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(minioProperties.getBucket())
                            .object(fileName)
                            .stream(inputStream,inputStream.available(),-1)
                            .build());
            //构建url
            url  = minioProperties.getEndpoint()+"/"+minioProperties.getBucket()+"/"+fileName;
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }

    @Override
    public String upLoadLogo(MultipartFile file) {
        String url = null;
        try {
            //获取上传文件流
            InputStream inputStream = file.getInputStream();
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint(minioProperties.getEndpoint())
                            .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                            .build();
            // 检测桶是否存在
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucket()).build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucket()).build());
            } else {
                System.out.println("Bucket "+minioProperties.getBucket()+" already exists.");
            }

            String dirPath =  DateUtil.format(new Date(),"yyyyMMdd")+"/brandLogo" ;
            String fileName  =  dirPath+"/"+ UUID.randomUUID().toString().replace("-","")+file.getOriginalFilename();
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(minioProperties.getBucket())
                            .object(fileName)
                            .stream(inputStream,inputStream.available(),-1)
                            .build());
            //构建url
            url  = minioProperties.getEndpoint()+"/"+minioProperties.getBucket()+"/"+fileName;
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }
    }

